#!/usr/bin/env python
#coding: iso-8859-1

"""Documentation for enemy.py

Details and summary here.

project -- Rez: The Clone
author -- Edwood Grant
organization -- Binhex Tw.
version -- 1.00
since -- 7/07/2007 11:20:27
"""

#Standard imports
from direct.actor import Actor
from direct.interval.IntervalGlobal import *
from direct.particles.ParticleEffect import ParticleEffect
from direct.particles.Particles import Particles
import globals
from pandac.PandaModules import *
from random import random
    #@UnusedImport

def AnimLoop(actor, fromanim, toanim, rate=1, part=None):
    actor.enableBlend()
    actor.loop(fromanim)
    actor.setPlayRate(rate, fromanim)
    int = LerpAnimInterval(actor, 0.25, fromanim, toanim)
    int.start()

class EnemyBoss:
    def __init__(self):
        """Constructor
        We are not doing anything special here, only when we decide to init
        right here we are only creating some variables
        """
        print("init object_____")
        self.actor = Actor.Actor();
        self.node = render.attachNewNode("BigBoss")
        self.model = None
        self.time = None
        self.startPos = None
        self.stageBelong = None
        self.points = []
        self.times = []
        self.hits = []
        self.anim = []
        self.sound = None
        self.pos = None
        self.dead = False
        self.scorePoints = 0
        self.loop = False
        self.duration = 0
        self.channel = 0
        self.velocity = 0
        self.pitch = 0
        self.scale = 0
        self.idle_anim = None
        self.run_anim = None
        self.attack_anim = None
        self.dead_anim = None
        self.hit_anim = None
        self.life = 1
        #self.hits = 1
        self.num_hits = 0;
        self.current_transition = 0
        self.mySeq = Sequence()
        self.int = Sequence()
        self.old_anim = None

    def init(self):
        taskMgr.doMethodLater(self.time, self._init, 'Start-Later-Enemy')


        # Defining and loading the actor
    def _init(self, Task):

        self.actor.loadModel(self.model)

        self.actor.loadAnims({"idle":self.idle_anim})
        self.actor.loadAnims({"run":self.run_anim})
        self.actor.loadAnims({"attack":self.attack_anim})
        self.actor.loadAnims({"dead":self.dead_anim})
        self.actor.loadAnims({"hit":self.hit_anim})

        self.actor.bindAnim('run')
        self.actor.loop('run')
        self.actor.setBlend(frameBlend=True)
        self.actor.setPlayRate(1.5 + (globals.RATE - 1), 'run')
        self.actor.setPos(self.startPos)
        self.actor.reparentTo(self.node)
        self.actor.setColor(1.0, 1.0, 1.0, 1.0)
        self.actor.setScale(self.scale)


        self.actor.hitpoint1 = self.actor.exposeJoint(None, 'modelRoot', 'hitpoint1')
        self.actor.hitpoint2 = self.actor.exposeJoint(None, 'modelRoot', 'hitpoint2')
        globals.ENEMYPOINT = self.actor.hitpoint2
        self.cNode = CollisionNode('enemyTestCol')
        self.cNode.setIntoCollideMask(BitMask32.bit(1))
        self.cNode.setFromCollideMask(BitMask32.allOff())
        self.cNode.addSolid(CollisionSphere(0, 0, 0, 25))
        self.cNodePath = self.actor.hitpoint1.attachNewNode(self.cNode)
        self.cNodePath.show()
        self.cNodePath2 = self.actor.hitpoint2.attachNewNode(self.cNode)
        self.cNodePath2.show()
        

        print self.num_hits, self.hits[self.current_transition], self.current_transition
        if self.num_hits >= self.hits[self.current_transition]:
            self.int = self.actor.posInterval(self.times[self.current_transition], self.points[self.current_transition])
            self.actor.disableBlend()
            self.actor.loop('hit')
            self.num_hits = 0
            self.int.start()
            print self.current_transition

        self.actor.enableBlend()
        #self.updateTask = taskMgr.add(self._update, 'EnemyTest-Update')
        return Task.done


    def _update(self, task):
        globals.BIGBOSS = str(
                              str(self.current_transition) +
                              str(len(self.points)) +
                              str(self.num_hits) +
                              str(self.bb_hits) +
                              str(self.hits[self.current_transition]))
        
        return task.cont
    


    def die(self):
        globals.SCORE += self.scorePoints
        self.num_hits += 1
        self.old_anim = str(self.anim[self.current_transition]);

        print str(self.current_transition), len(self.points), str(self.num_hits), str(self.hits[self.current_transition])
        print self.times[self.current_transition], self.points[self.current_transition]
        
        if self.num_hits > self.hits[self.current_transition]:
            self.num_hits = 0
            self.current_transition += 1
            #we just changed state

            if self.current_transition > len(self.points)-1:
                globals.SCORE += self.scorePoints
                print globals.SCORE
                #Either way we remove the node and delete ourselves
                globals.STAGENUM += 1
                globals.ENEMYPOINT = None

                self.node.removeNode()
                del self
                return 0

            self.mySeq.finish()
            self.int = self.node.posInterval(self.times[self.current_transition], self.points[self.current_transition])

            self.actor.loop(str(self.anim[self.current_transition]))
            self.mySeq = Sequence((LerpAnimInterval(self.actor, 4.5, 'hit', self.old_anim)), LerpAnimInterval(self.actor, 1.0, self.old_anim, str(self.anim[self.current_transition])))
            self.mySeq.start()
            self.int.start()

            print globals.SCORE, self, str(self.anim[self.current_transition])


        else:
            #just make one hit
            self.mySeq.finish()
            self.actor.loop(self.old_anim)
            self.mySeq = Sequence(LerpAnimInterval(self.actor, 2.0, 'hit', self.old_anim, 0.2))
            #self.mySeq = Sequence( (LerpAnimInterval(self.actor, 0.25,  self.old_anim,'hit')) , LerpAnimInterval(self.actor, 0.5,'hit', self.old_anim))

            #LerpAnimInterval(self.actor, 0.25,self.actor.getCurrentAnim(), str(self.anim[self.current_transition])));
            self.mySeq.start()
            print self.old_anim,
